package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.ProgramaDAO;
import com.cs.beneficios.business.entity.SubProgramaDAO;
import com.cs.beneficios.business.entity.UsuarioSubprogramaDAO;
import com.cs.beneficios.business.model.ProgramaVO;
import com.cs.beneficios.business.model.SubProgramaVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;
//import com.cs.paoyer.administration.exception.AdministrationUserException;
//import com.cs.paoyer.administration.util.AccessControl;
//import com.cs.paoyer.common.exception.CommonUserException;
//import com.cs.paoyer.common.exception.UserException;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;
//import org.apache.struts.action.ActionMessages;
//import org.apache.struts.action.ActionMessage;
public class SelectionPermissionReportAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
 
        HttpSession session = request.getSession();
        UserVO user = (UserVO)session.getAttribute("userInfo");
        //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	    ConnectionFactory cf = new ConnectionFactory();
        String strprog = request.getParameter("codePrograma");
        String strarea = request.getParameter("codeArea");
        List lSecretarias = new LinkedList();
        List lProgramas = new LinkedList();
        List lSubprog = new LinkedList();
        List lUsuSeleccion = new LinkedList();
        //Proceso para guardar los usuarios seleccionados
        List lUsuarios = new LinkedList();
        
        lUsuarios = (List)session.getAttribute("ListaUsuarios");
        for(int i=0;i<lUsuarios.size();i++)
        {
          String seleccion = "codeusuario"+i;
          Object ocoduser = request.getParameter(seleccion);
          if(ocoduser!=null)
          {
            lUsuSeleccion.add((String)ocoduser);
          }
        }
        //Fin del proceso
        if(lUsuSeleccion.size()>0)
          session.setAttribute("ListadeUsuarios",lUsuSeleccion);
        try
        {
            UsuarioSubprogramaDAO objUsuSubpro = new UsuarioSubprogramaDAO(cf);
            lSecretarias = (List)objUsuSubpro.getSecretaria();
            cf.closeConnection(objUsuSubpro.getConn());
            if(lSecretarias.size() > 0)
            {
              request.setAttribute("secretarias",lSecretarias);
            }else
            {
              request.setAttribute("msg","No hay secretarias inscritas.");
            }
            if((Object)strarea != null && !strarea.equals("nulo"))
            {
              ProgramaDAO objProgramaDAO = new ProgramaDAO(cf);
              ProgramaVO objProgramaVO = new ProgramaVO();
              objProgramaVO.setCodeArea(new Long(strarea));
              lProgramas = (List)objProgramaDAO.findProgramaByCriterea(objProgramaVO, user.getUserGroupName(),user.getCode(),user.getCodeArea());
              cf.closeConnection(objProgramaDAO.getConn());
              if(lProgramas.size() > 0)
              {
                request.setAttribute("programas",lProgramas);
              }else
              {
                request.setAttribute("msg","La Secretaria no posee programas inscritos.");
              }
              
              if((Object)strprog != null && !strprog.equals("nulo"))
              {
                SubProgramaDAO objSubProgramaDAO = new SubProgramaDAO(cf);
                SubProgramaVO objSubProgramaVO =  new SubProgramaVO();
                objSubProgramaVO.setCodePrograma(new Long(strprog));
                lSubprog = (List)objSubProgramaDAO.findSubProgramaByCriterea(objSubProgramaVO,user.getCode(), user.getUserGroupName(),user.getCodeArea());
                cf.closeConnection(objSubProgramaDAO.getConn());
                if(lSubprog.size() > 0)
                {
                  request.setAttribute("subprogramas",lSubprog);
                  int tamanosubpro = lSubprog.size();
                  session.setAttribute("TamanoSubpro",String.valueOf(tamanosubpro));
                }else
                {
                  request.setAttribute("msg","El programa no tiene beneficios inscritos.");
                }
              }
            }
        }catch(SQLException e)
        {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
  
    return mapping.findForward("success");
  }
}